<?php

$order = $_SESSION['paging'][$path_string]['order'];
$filter = isset($_SESSION['paging'][$path_string]['filter']) ? '%' . $_SESSION['paging'][$path_string]['filter'] . '%' : '%';

if ($_REQUEST['id']) {
	$_SESSION['paging'][$path_string]['category_id'] = $_REQUEST['id'];
	$_SESSION['paging'][$path_string]['page'] = 1;
}

$sql = $db->select()
   ->from(array('p' => 'shop_products'), '*')
   ->join(array('cp' => 'shop_categories_products'), 'p.id=cp.product_id')
   ->order($order)
   ->where('parent_id IS NULL')
   ->where('deleted=0')
   ->where($db->quoteInto('p.site_id=?', $site['id']))
   ->where($db->quoteInto('cp.category_id=?', $_SESSION['paging'][$path_string]['category_id']))
   ->group('p.id')
   ->where($db->quoteInto('code LIKE ? OR title LIKE ? OR description LIKE ?', $filter))
;
#echo $sql;

$products = $db->fetchAll($sql);


$page   = $_SESSION['paging'][$path_string]['page'];
$show   = $_SESSION['paging'][$path_string]['show'];
$offset = ($page - 1) * $show;
$total  = count($products);
#
$smarty->assign('order', $order);
$smarty->assign('page', $page);
$smarty->assign('show', $show);
$smarty->assign('filter', $filter);
$smarty->assign('offset', $offset);
$smarty->assign('total', $total);

$products = array_slice($products, $offset, $show);
#
#
require $app_root . '/admin/frontend/_modules/shop/includes/Product.class.php';
require $app_root . '/admin/frontend/_modules/shop/includes/functions.php';
require $app_root . '/admin/frontend/_modules/shop/includes/modifier.format_price.php';
#

#print_r($products);
#$products = array_map(create_function('$product', '$p = new Product($product["id"]); return $p->export();'), $products);

$smarty->assign('products', $products);

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
	header('Content-type: text/xml');
	$smarty->display('shop/products/index.tpl.taconite');
} else {
	$smarty->display('shop/products/index.tpl.html');
}